using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;

namespace Brick
{
	public class LevelMake : MonoBehaviour
	{
		public GameObject level;

		public GameObject v;

		public GameObject h;

		public Image[] tfV;

		public Image[] tfH;

		public GameObject item;

		public itemMake[] items;

		public Image image;

		public Sprite[] sp;

		public InputField intputFieldSelect;

		public InputField intputFieldLevel;

		public InputField intputFieldStar1;

		public InputField intputFieldStar2;

		public InputField intputFieldStar3;

		public InputField intputFieldLevelIn;

		public itemMake selectItem;

		public chooseitem curChoose;

		public LevelData loadLevelData;

		public bool bPlaying;

		private bool bDownSix;

		private void Start()
		{
			tfV = v.GetComponentsInChildren<Image>();
			tfH = h.GetComponentsInChildren<Image>();
		}

		private void Update()
		{
			if (!bPlaying)
			{
				return;
			}
			if (Input.GetMouseButtonDown(0))
			{
				Vector2 point = Camera.main.ScreenToWorldPoint(UnityEngine.Input.mousePosition);
				if (items.Length > 0)
				{
					for (int i = 0; i < items.Length; i++)
					{
						itemMake itemMake = items[i];
						Rect rect = default(Rect);
						Vector3 position = itemMake.transform.position;
						rect.x = position.x - 32.5f;
						Vector3 position2 = itemMake.transform.position;
						rect.y = position2.y - 32.5f;
						rect.width = 65f;
						rect.height = 65f;
						if (rect.Contains(point))
						{
							selectItem = itemMake;
							Item item = curChoose.GetItem();
							image.sprite = curChoose.getCurSP();
							itemMake.setImage(curChoose.getCurSP());
							itemMake.setNum(item.num);
							itemMake.setOpen(curChoose.getOpen());
							itemMake.initData(item.type, item.ID);
							break;
						}
					}
				}
			}
			if (Input.GetMouseButton(0))
			{
				Vector2 point2 = Camera.main.ScreenToWorldPoint(UnityEngine.Input.mousePosition);
				if (items.Length > 0)
				{
					for (int j = 0; j < items.Length; j++)
					{
						itemMake itemMake2 = items[j];
						Rect rect2 = default(Rect);
						Vector3 position3 = itemMake2.transform.position;
						rect2.x = position3.x - 32.5f;
						Vector3 position4 = itemMake2.transform.position;
						rect2.y = position4.y - 32.5f;
						rect2.width = 65f;
						rect2.height = 65f;
						if (rect2.Contains(point2))
						{
							selectItem = itemMake2;
							Item item2 = curChoose.GetItem();
							image.sprite = curChoose.getCurSP();
							itemMake2.setImage(curChoose.getCurSP());
							itemMake2.setNum(item2.num);
							itemMake2.setOpen(curChoose.getOpen());
							itemMake2.initData(item2.type, item2.ID);
							break;
						}
					}
				}
			}
			if (!Input.GetMouseButtonUp(0))
			{
			}
		}

		public void onStart()
		{
			bPlaying = false;
			loadLevelData = null;
			if (!(intputFieldLevelIn.text == string.Empty))
			{
				LoadbyJson(int.Parse(intputFieldLevelIn.text));
			}
			int num = 1;
			if (items.Length > 0)
			{
				itemMake[] array = items;
				foreach (itemMake itemMake in array)
				{
					itemMake itemMake2 = itemMake;
					DataItem itemData = getItemData(num);
					if (itemData != null)
					{
						itemMake2.GetComponent<itemMake>().initData(itemData.type, itemData.typeID);
						itemMake2.GetComponent<itemMake>().setNum(itemData.num);
						itemMake2.GetComponent<itemMake>().setImage(curChoose.getSprite(itemData.typeID, itemData.type));
					}
					else
					{
						itemMake2.GetComponent<itemMake>().initData(0, 0);
						itemMake2.GetComponent<itemMake>().setNum(0);
						itemMake2.GetComponent<itemMake>().setImage(curChoose.getSprite(0, 0));
					}
					num++;
				}
			}
			else
			{
				for (int j = 0; j < tfH.Length; j++)
				{
					Vector2 vector = tfH[j].transform.position;
					for (int k = 0; k < tfV.Length; k++)
					{
						Vector2 vector2 = tfV[k].transform.position;
						GameObject gameObject = UnityEngine.Object.Instantiate(item);
						gameObject.transform.position = new Vector2(vector2.x, vector.y);
						gameObject.transform.SetParent(level.transform);
						gameObject.GetComponent<itemMake>().setID(num);
						DataItem itemData2 = getItemData(num);
						if (itemData2 != null)
						{
							gameObject.GetComponent<itemMake>().initData(itemData2.type, itemData2.typeID);
							gameObject.GetComponent<itemMake>().setNum(itemData2.num);
							gameObject.GetComponent<itemMake>().setImage(curChoose.getSprite(itemData2.typeID, itemData2.type));
						}
						num++;
					}
				}
			}
			items = level.GetComponentsInChildren<itemMake>();
			bPlaying = true;
		}

		public void LoadbyJson(int level)
		{
			string path = Application.dataPath + "/StreamingAssets/Level/Level_" + level.ToString() + "_cfg.json";
			if (!File.Exists(path))
			{
				return;
			}
			StreamReader streamReader = new StreamReader(path);
			if (streamReader != null)
			{
				string text = streamReader.ReadToEnd();
				if (text.Length > 0)
				{
					LevelData levelData = loadLevelData = JsonUtility.FromJson<LevelData>(text);
					intputFieldLevel.text = levelData.Level;
					intputFieldStar1.text = levelData.Star[0].ToString();
					intputFieldStar2.text = levelData.Star[1].ToString();
					intputFieldStar3.text = levelData.Star[2].ToString();
				}
			}
			streamReader.Close();
		}

		public DataItem getItemData(int k)
		{
			if (loadLevelData != null)
			{
				foreach (DataItem item2 in loadLevelData.Stage)
				{
					if (bDownSix)
					{
						if (item2.ID + 66 == k)
						{
							return item2;
						}
					}
					else if (item2.ID == k)
					{
						return item2;
					}
				}
			}
			return null;
		}

		private LevelData CreateSaveGO()
		{
			LevelData levelData = new LevelData();
			levelData.Level = intputFieldLevel.text;
			levelData.Star = new List<int>();
			int num = int.Parse(intputFieldStar1.text);
			levelData.Star.Add(num);
			levelData.Star.Add(int.Parse(intputFieldStar2.text));
			levelData.Star.Add(int.Parse(intputFieldStar3.text));
			levelData.Stage = new List<DataItem>();
			itemMake[] array = items;
			foreach (itemMake itemMake in array)
			{
				itemMake itemMake2 = itemMake;
				if (itemMake2.type != 0)
				{
					DataItem dataItem = new DataItem();
					dataItem.ID = itemMake2.ID;
					dataItem.type = itemMake2.type;
					dataItem.typeID = itemMake2.typeID;
					dataItem.num = itemMake2.num;
					dataItem.bOpen = itemMake2.defaultOpen;
					levelData.Stage.Add(dataItem);
				}
			}
			return levelData;
		}

		private void SaveByJson()
		{
			LevelData obj = CreateSaveGO();
			string path = Application.dataPath + "/StreamingAssets/out/Level_" + intputFieldLevel.text + "_cfg.json";
			string value = JsonUtility.ToJson(obj);
			StreamWriter streamWriter = new StreamWriter(path);
			streamWriter.Write(value);
			streamWriter.Close();
		}

		public void OnClickSave()
		{
			SaveByJson();
		}

		public void OnCleanSelect()
		{
		}

		public void OnCleanAll()
		{
		}

		public void OnDownSix()
		{
			bDownSix = !bDownSix;
		}
	}
}

